package com.atguigu.gmall.index.config;

import com.atguigu.gmall.common.bean.ResponseVo;
import com.atguigu.gmall.index.feign.GmallPmsClient;
import com.atguigu.gmall.pms.entity.CategoryEntity;
import org.redisson.api.RBloomFilter;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.List;

/**
 * @author Jia Fa
 * @create 2021-12-13 20:55
 */
//@Configuration
public class bloomTest {

    @Autowired
    private RedissonClient redissonClient;
    @Autowired
    private GmallPmsClient pmsClient;

    private static final String INDEX_PREFIX = "index:cates:";

    @Bean
    public RBloomFilter<String> bloomFilter(){
        // 这里存储的是所有一级分类的id值
        RBloomFilter<String> bloomFilter = this.redissonClient.getBloomFilter("index:bloom:filter");
        // 初始化误差率
        bloomFilter.tryInit(30, 0.01);
        // 将查询到的值设置到布隆过滤器
        ResponseVo<List<CategoryEntity>> listResponseVo = this.pmsClient.categoryByPid(0l);
        List<CategoryEntity> categoryEntities = listResponseVo.getData();
        // 将一级分类的id全部存入
        categoryEntities.forEach(categoryEntity -> {
            // 注意 保存到布隆过滤器中的key值需要与注解中设置的key值相同 否则查询为null
            bloomFilter.add(INDEX_PREFIX+"["+categoryEntity.getId()+"]");
        });
        return bloomFilter;

    }
}
